/*
 * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.mhb.generator.config.po;

import com.mhb.generator.config.rules.FileType;
import com.mhb.generator.util.CollectionUtils;
import com.mhb.generator.util.StringUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.File;
import java.io.Serializable;
import java.util.*;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

/**
 * @author 博博
 * @time 2019/12/27
 */
@Data
@Accessors(chain = true)
public class TableInfo {
    // 需要导入的包
    private final Map<String, Set<String>> importPackages = new HashMap<>();
    // 包名
    private final Map<String, String> packageNames = new HashMap<>();
    // 数据库表名
    private String name;
    // 注释
    private String comment;
    // 实体名
    private String entityName;
    // mapper名
    private String mapperName;
    // xml名
    private String xmlName;
    // service名
    private String serviceName;
    // 实现类名
    private String serviceImplName;
    // 控制类名
    private String controllerName;
    // 视图模板
    private String selectViewName;
    private String updateViewName;
    private String detailViewName;
    private String saveViewName;
    // 字段集合
    private List<TableField> fields;

    // 主键
    private String primaryKey;
    private String primaryKeyName;




    public String getCapitalEntityName() {
        return StringUtil.firstToLowerCase(this.entityName);
    }

    public String getCapitalPrimaryKeyName() {
        return StringUtil.format("#{%s}",primaryKeyName);
    }

    public String getCapitalServiceName() {
        return StringUtil.firstToLowerCase(this.serviceName);
    }

    public String getCapitalMapperName() {
        return StringUtil.firstToLowerCase(this.mapperName);
    }



    /**
     * 将字段转换为','分割的字符串
     *
     * @return 转换后的字符串
     */
    public String getFieldsSeparation() {
        String str = "";
        if (CollectionUtils.isNotEmpty(fields)) {
            str = fields.stream().map(TableField::getColumn).map(StringUtil::quotaTranslate).collect(joining(","));
        }
        return str;
    }

    public String getFieldsSaveBach(){
        String str = "";
        if (CollectionUtils.isNotEmpty(fields)) {
            str = fields.stream().map(TableField::getXmlListEntryParameters).collect(joining(","));
        }
        return str;
    }

    public String getFieldsSaveItemBach(){
        String str = "";
        if (CollectionUtils.isNotEmpty(fields)) {
            str = fields.stream().map(TableField::getXmlEntryParameters).collect(joining(","));
        }
        return str;
    }

    public String getFieldUpdateBach(){
        String str = "";
        if (CollectionUtils.isNotEmpty(fields)) {
            str = fields.stream().filter(tableField -> !tableField.isKeyFlag()).map(TableField::getXmlUpdateEntryParameters).collect(joining(","));
        }
        return str;
    }
}
